package com.gxa.controller.discrepancy;

import com.gxa.common.utils.R;
import com.gxa.entity.discrepancy.Dept;
import com.gxa.entity.discrepancy.Device;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */
@Api(tags = "部门")
@RestController
@RequestMapping("/dept")
public class DeptController {
    private List<Dept> list;
    public DeptController(){
        this.init();
    }
    void init(){
        this.list = new ArrayList<>();
        Dept dept1 = new Dept();
        dept1.setId(1);
        dept1.setDeptName("开发部");
        Dept dept2 = new Dept();
        dept2.setId(2);
        dept2.setDeptName("人力资源部");



        list.add(dept1);
        list.add(dept2);
    }


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("设备列表接口")
    @GetMapping("/list")
    public R queryAll(){

        return ok("successs").put("data",list);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增设备接口")
    @PostMapping("/add")
    public R add(@RequestBody Dept device){
        this.list.add(device);
        return ok("add success").put("data",list);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除设备接口")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id){
        for(Dept d:list){
            if(d.getId()== id){
                list.remove(d);
                return R.ok().put("data",list);
            }
        }

        return ok("del error");
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询设备接口")
    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable("id") Integer id){
        for(Dept d:list){
            if (id == d.getId()){
                return ok("queryById").put("data",d);
            }
        }
        return error("query error");
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑设备接口")
    @PutMapping("/edit")
    public R edit(@RequestBody Dept device){
        for(Dept d:list){
            if(d.getId()== device.getId()){
                d = device;
                return R.ok().put("data",device);
            }
        }
        return ok("edit error");
    }
}
